<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 动态接口
 */
class Blog extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getBlogList()
    {
        if ($this->request->isPost()) {
            $pageIndex = $this->request->post('pageIndex', 1);
            $pageSize = $this->request->post('pageSize', 10);
            $keyword = $this->request->post('keyword');
            $user = $this->auth->getUser();
            $where = [];
            $where2 = [];
            if ($user) {
                // 普通用户
                if ($user['group_id'] == 2) {
                    $where = 'jb.`STATUS` = 1 OR jb.`STATUS` = 2';
                }
            }
            if ($keyword) {
                $where2 = '(jb.blog_title LIKE concat("%","' . $keyword . '","%") 
                or jb.sub_title LIKE concat("%","' . $keyword . '","%") 
                or jb.text_content LIKE concat("%","' . $keyword . '","%"))';
            }
            $list = model('\app\admin\model\jy\Blog')->alias('jb')
                ->field('jb.*')
                ->where('jb.is_del = 0')
                ->where($where)
                ->where($where2)
                ->order('status desc, public_time desc')
                ->paginate($pageSize, false, ['page' => $pageIndex]);
            foreach ($list->items() as &$item) {
                if ($item['avatar']) {
                    $item['avatar'] = cdnurl($item['avatar']);
                }
                $imagesList = [];
                if ($item['blog_images']) {
                    $imagesStrList = explode(',', $item['blog_images']);
                    $item['imageCount'] = sizeof($imagesStrList);
                    if ($imagesStrList) {
                        foreach ($imagesStrList as &$image) {
                            $imagesList[] = cdnurl($image);
                        }
                    }
                    if (sizeof($imagesList) >= 4) {
                        $imagesList = array_slice($imagesList, 0, 4);
                    } else {
                        $imagesList = array_slice($imagesList, 0, 1);
                    }
                }
                $item['imageList'] = $imagesList;

                // 是否点赞
                if ($this->auth) {
                    $praise = model('\app\admin\model\jy\BlogPraise')->where(['user_id' => $this->auth->id, 'blog_id' => $item['id']])->find();
                    if ($praise) {
                        $item['isPraise'] = 1;
                    } else {
                        $item['isPraise'] = 0;
                    }
                } else {
                    $item['isPraise'] = 0;
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            $this->success('', $result);
        }
    }

    public function getBlogById($blogId = NULL)
    {
        if (!$blogId) {
            $this->error(__('Parameter %s can not be empty', 'blogId'));
        }
        $blog = model('\app\admin\model\jy\Blog')->where('id', $blogId)->find();
        if (!$blog) {
            $this->error(__('No Results were found'));
        }
        if ($blog['avatar']) {
            $blog['avatar'] = cdnurl($blog['avatar']);
        }
        $imagesList = [];
        if ($blog['blog_images']) {
            $imagesStrList = explode(',', $blog['blog_images']);
            if ($imagesStrList) {
                foreach ($imagesStrList as &$image) {
                    $imagesList[] = cdnurl($image);
                }
            }
        }
        $blog['imageList'] = $imagesList;
        // 获取浏览人
        $userList = model('\app\admin\model\jy\BlogScan')->alias('jbs')
            ->field('ju.id, ju.nickname, ju.avatar')
            ->join('jy_user ju', 'jbs.user_id = ju.id')
            ->where(['jbs.blog_id' => $blogId])
            ->order('jbs.create_time desc')
            ->select();
        if ($userList) {
            foreach ($userList as &$item) {
                if ($item['avatar']) {
                    $item['avatar'] = cdnurl($item['avatar']);
                }
            }
            if (sizeof($userList) >= 3) {
                $userList = array_slice($userList, 0, 3);
            }
            $blog['userList'] = $userList;
        } else {
            $blog['userList'] = [];
        }
        // 是否点赞
        $praise = model('\app\admin\model\jy\BlogPraise')->where(['user_id' => $this->auth->id, 'blog_id' => $blogId])->find();
        if ($praise) {
            $blog['isPraise'] = 1;
        } else {
            $blog['isPraise'] = 0;
        }
        $this->success('', $blog);
    }
}